Reverse engineer the encryption algorithm
A secret agent has intercepted an encrypted message from a rival organization. The encryption algorithm and the encrypted output are provided below. Your mission is to reverse the encryption process and decrypt the message to retrieve the flag.
Objective: Analyze the Python encryption code, understand the algorithm, and write a decryption function to reveal the hidden flag.
def encrypt(message):
# Custom encryption algorithm
import base64
# Step 1: Reverse the string
step1 = message[::-1]
# Step 2: XOR each character with its position (shifted by 7)
step2 = ""
for i, char in enumerate(step1):
xor_value = ord(char) ^ ((i + 7) % 256)
step2 += chr(xor_value)
# Step 3: Rotate characters by 13 positions (ROT13 variant)
step3 = ""
for char in step2:
if char.isalpha():
base = ord('A') if char.isupper() else ord('a')
step3 += chr(((ord(char) - base + 13) % 26) + base)
else:
step3 += char
# Step 4: Base64 encode
step4 = base64.b64encode(step3.encode()).decode()
return step4
# Original flag (hidden from you)
# flag = "CTF{r3v3rs3_3ngine3r1ng_pyth0n_pr0}"
# encrypted = encrypt(flag)
# print(encrypted)